<?php 

class MemberControl extends CommonControl{
	public  function __construct(){
		parent::__construct();
		$this->left_info();
		$this->top_cate();
	}
	public function index(){
		$uid=$_GET['uid'];
		// 获得当前用户提问数目
		$field='title,ask.asid,ask.time,ask.content,answer';
		$sql="SELECT {$field} FROM hd_ask as ask join hd_category as cate on ask.cid=cate.cid where uid={$uid} limit 5";
		$my_ask=M('category')->query($sql);
		if(!empty($my_ask)&&isset($my_ask)){
			foreach ($my_ask as $k => $v) {
				$my_ask[$k]['time']=date('Y-m-d',$v['time']);
			}
			$this->assign('my_ask',$my_ask);
		};
		//获得当前用户提问数目
		$sql1="SELECT {$field} FROM hd_answer as an join hd_ask as ask on an.asid=ask.asid join hd_category as cate on ask.cid=cate.cid where an.uid={$uid} limit 5";
		$my_answer=M('category')->query($sql1);
		if(!empty($my_answer)&&isset($my_answer)){
		$this->assign('my_answer',$my_answer);
		};
		
	$this->display();
	}
	public  function my_answer(){
		$uid=$_GET['uid'];
		$h=isset($_GET['h'])?(int)$_GET['h']:0;
		$field='ask.content,ask.answer,ask.time,ask.asid,title';
		$limit=4;
		switch ($h) {
			case 0:
				$sql="SELECT {$field} from hd_answer as an join hd_ask as ask on an.asid=ask.asid join hd_category as cate on ask.cid=cate.cid where an.uid={$uid} limit {$limit} ";
				break;
			case 1:
				$sql="SELECT {$field} from hd_answer as an join hd_ask as ask on an.asid=ask.asid join hd_category as cate on ask.cid=cate.cid where an.uid={$uid} and accept=1 limit {$limit} ";
				break;
			
			default:
				$sql="SELECT {$field} from hd_answer as an join hd_ask as ask on an.asid=ask.asid join hd_category as cate on ask.cid=cate.cid where an.uid={$uid}  limit {$limit}";
				$_GET['h']=0;
				break;
		}
		$answer_ask=M('category')->query($sql);
		// var_dump($answer_ask);
		if(!is_null($answer_ask))$this->assign('answer_ask',$answer_ask);
		$this->display();
	}
	public function my_ask(){
		$uid=$_GET['uid'];
		$w=isset($_GET['w'])?(int)$_GET['w']:0;
		switch ($w) {
			case 0:
				$where=M('ask')->where('solve=0 and uid='.$uid);
				break;
			case 1:
				$where=M('ask')->where('solve=1 and uid='.$uid);
				break;
			
			default:
				$where=M('ask')->where('solve=0 and uid='.$uid);
				$_GET['w']=0;
				break;
		}
		$field='content,answer,time,asid,cid';
		$my_solve=$where->limit(5)->field($field)->all();
		if(!is_null($my_solve))
			foreach ($my_solve as $k => $v) {
				$title=M('category')->field('title')->where('cid='.$v['cid'])->find();
				$my_solve[$k]['title']=$title['title'];
				$this->assign('my_solve',$my_solve);
			}
		$this->display();
	}
	public function my_face(){

		if($_GET['uid']!=$_SESSION['uid'])$this->go(__ROOT__);
		$uid=(int)$_SESSION['uid'];
		if(!empty($_FILES)){

			$upload=new Upload;
			$up=$upload->up();
			// 数据库拿到旧的图像地址
			// var_dump($up);
			$oldface=M('user')->where('uid='.$uid)->field('face')->find();
			
			
			if(isset($up[0])){
				// 判断如果返回真就处理地址
			$face_addr=dirname(__ROOT__).ltrim($up[0]['upload'],'.');
			// 更改数据库图像地址
			$face=M('user')->where('uid='.$uid)->update(array('face'=>$face_addr));
			// var_dump($face_addr);die;
			// 如果数据库写入成功并且原来的旧地址存在就执行删除原来图片
			}
			if(isset($face)&&isset($oldface)&&!empty($oldface)){
				// 转换成相对地址
				$oldface='./'.ltrim($oldface['face'],dirname(__ROOT__));
				@unlink($oldface);
			}
			$this->left_info();
		}
		$this->display();
	}
	public function my_gold(){
		$this->display();
	}
	public function my_level(){

		$this->display();
	}

	/**
	 * [left_info 左侧用户信息]
	 * @return [type] [description]
	 */
	public function left_info(){
		if(!isset($_GET['uid'])||empty($_GET['uid']))$this->error('用户不存在');
			$uid = (int)$_GET['uid'];
			$field = 'uid,face,exp,point,answer,ask,accept,username';
			$userInfo = M('user')->where('uid='.$uid)->field($field)->find();
			if(is_array($userInfo)){
			$userInfo['face'] = $this->face($userInfo);
			$userInfo['level'] = $this->exp_to_level($userInfo);
			$userInfo['ratio'] = (int)$this->ratio($userInfo);

			$this->assign('userInfo', $userInfo);
			}else{
				$this->back();
			}
			//第三人称
			$rank = isset($_SESSION['uid']) && $uid == $_SESSION['uid'] ? '我' : 'TA';
			$this->assign('rank', $rank);
		
	}

}
 ?>